<template>
  <div class="app-container">
    <el-card shadow="hover">
      <div>
        <i class="el-icon-tickets"></i>
        <span> 战略合作伙伴</span>
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="small"
          @click="handleAdd"
          style="margin-left: 20px;"
          >新增</el-button
        >
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="small"
          :disabled="multipleList"
          @click="handleDeleteNews"
          >批量删除</el-button
        >
      </div>
      <el-table
        v-loading="loading1"
        :data="newsList"
        @selection-change="handleSelectionChange1"
        border
        style="margin-top: 10px;"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="合作伙伴Logo" align="center">
          <template slot-scope="scope">
            <img style="height: 80px;max-width:280px" :src="showUrl + scope.row.pic" />
          </template>
        </el-table-column>
        <el-table-column
          label="跳转地址"
          align="center"
        >
            <template slot-scope="scope"> 
                <a :href="scope.row.links" target="_blank">{{scope.row.links}}</a>
            </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          fixed="right"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              >修改</el-button
            >
            <el-button
              size="mini"
              type="danger"
              icon="el-icon-delete"
              @click="handleDeleteNews(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="newsTotal > 0"
        :total="newsTotal"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>

    <!-- 添加或修改 资讯文章 -->
    <el-dialog :title="title" :visible.sync="open" width="65%">
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-row>
          
          <el-col :span="24">
            <el-form-item label="合作伙伴Logo:" prop="pic">
              <el-upload
                class="avatar-uploader"
                :action="upImage + '/cms/company/upload'"
                :show-file-list="false"
                :headers="headers"
                :on-success="handleAvatarSuccess"
              >
                <img
                  v-if="form.pic"
                  :src="showUrl + form.pic"
                  class="avatar"
                />
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="跳转地址:">
              <el-input v-model="form.links" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitNewsForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  queryFriends,
  addFriends,
  updateFriends,
  delFriends,
  gettypelist
} from "@/api/web/school";
import Editor from "@/components/Editor";
import { getToken } from "@/utils/auth";

export default {
  name: "Friend",
  components: {
    Editor
  },
  data() {
    return {
      headers: { Authorization: "Bearer " + getToken() },
      upImage: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading1: true,
      // 选中数组
      all: [],
      // 非单个禁用
      singleList: true,
      // 非多个禁用
      multipleList: true,
      newsTotal: 0,
      
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        workInterval:''
      },
      newsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        pic: [
          { required: true, message: "合作伙伴logo不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 查询资讯百科文章
    getList() {
      this.loading1 = true;
      queryFriends(this.queryParams).then(res => {
        // console.log(res);
        this.newsList = res.rows;
        this.newsTotal = res.total;
        this.loading1 = false;
      });
    },
    // 点击新增
    handleAdd() {
      this.form = {
        pic: ""
      };
      this.open = true;
      this.title = "添加合作伙伴";
    },
    handleUpdate(row) {
      this.form = row;
      this.open = true;
      this.title = "编辑合作伙伴";
    },
    handleAvatarSuccess(res) {
      this.form.pic = res.data;
    },
    cancel() {
      this.open = false;
      this.getList();
    },
    // 提交资讯文章表单信息
    submitNewsForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateFriends(this.form).then(res => {
              if (res.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addFriends(this.form).then(res => {
              if (res.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    handleSelectionChange1(selection) {
      this.all = selection.map(item => item.id);
      this.singleList = selection.length != 1;
      this.multipleList = !selection.length;
    //   console.log(this.all)
    },
    handleDeleteNews(row) {
      const id = row.id || this.all;
      console.log(id)
      this.$confirm("是否确认删除?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return delFriends(id);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(function() {});
    }
  }
};
</script>

<style scoped>
.select {
  margin-left: 20px;
  width: 220px;
}
.avatar-uploader .el-upload {
  width: 148px;
  height: 148px;
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #20a0ff;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 148px;
  height: 148px;
  line-height: 148px;
  text-align: center;
  border: 1px dashed #ccc;
}

.avatar {
  width: 148px;
  height: 148px;
  display: block;
}
</style>
